package com.youhong.teethcare.plaque;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rmondjone.locktableview.CustomHorizontalScrollView;
import com.rmondjone.locktableview.DisplayUtil;
import com.youhong.teethcare.R;
import com.youhong.teethcare.javabeans.PlaqueRecord;
import com.youhong.teethcare.plaque.dialogs.BleedingDialog;
import com.youhong.teethcare.plaque.dialogs.PlaqueDialog;
import com.youhong.teethcare.plaque.dialogs.PocketDialog;
import com.youhong.teethcare.plaque.dialogs.ToothMobilityDialog;
import com.youhong.teethcare.utils.ScreenUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/9/5.
 */

public class PlaqueTableView implements View.OnClickListener {

    Context mContext;
    View mTableView;
    ViewGroup mContainerView;
    LinearLayout ll_columnHeader;
    RelativeLayout rl_columnInfoHeader;
    CustomHorizontalScrollView mScrollView;

    public void setmOnclickLiastener(View.OnClickListener mOnclickLiastener) {
        this.mOnclickLiastener = mOnclickLiastener;
    }

    View.OnClickListener mOnclickLiastener;

    public PlaqueTableView setColumnHeaderWidth(int mColumnHeaderWidth) {
        this.mColumnHeaderWidth = mColumnHeaderWidth;
        return this;
    }

    public PlaqueTableView setColumnHeaderTextSize(int mColumnHeaderTextSize) {
        this.mColumnHeaderTextSize = mColumnHeaderTextSize;
        return this;
    }

    private float mColumnHeaderWidth = 120; //Unit: DP
    private float mColumnHeaderTextSize = 14f; // Unit: SP
    private int mCellMargin = 5; // Unit: Dp
    private int mCellPadding = 8; // Unit: Dp
    private float mSplitLineThick = 0.7f;
    private float mSplitLineHeavyThick = mSplitLineThick * 3;

    private float drawableHeight = 28f;
    private float drawableWidth = 28f;
    private float mViewWidth = 80; //Cell View 的Width
    private int mViewHeight; //高度跟ColumnHeader 一致
    private float mFirstRowHeight = 20f;
    //对应每颗牙齿位置的View
    TextView[][] views = new TextView[8][16]; //行,列

    public PlaqueTableView(Context mContext, ViewGroup containerView) {
        this.mContext = mContext;
        this.mContainerView = containerView;
        initView();
    }

    private void initView() {
        mTableView = LayoutInflater.from(mContext).inflate(R.layout.view_tableview, null);
        ll_columnHeader = mTableView.findViewById(R.id.tableView_columnHeader);
        rl_columnInfoHeader = mTableView.findViewById(R.id.RelativeLayout_columnHeader);
        mScrollView = mTableView.findViewById(R.id.tableView_ScrollView);
    }

    String[] headerTexts = null;

    /**
     * 加载Column标题
     */
    private void loadColumnHeader(LinearLayout ll_columnHeader) {


        {
            /**
             * 画头部的空白
             */
            View v_header = new View(mContext);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mColumnHeaderWidth),
                    ScreenUtils.dip2px(mContext, mFirstRowHeight));
            //v_header.setBackgroundColor(Color.RED);
            v_header.setLayoutParams(layoutParams);
            ll_columnHeader.addView(v_header);

        }
        headerTexts = mContext.getResources().getStringArray(R.array.newstring_array_tableColumn);

        //获取TextView 中文字最长的Column 的高度
        TextView temp_tv = new TextView(mContext);
        temp_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, mColumnHeaderTextSize);
        temp_tv.setPadding(ScreenUtils.dip2px(mContext, mCellMargin), 0, 0, 0);
        LinearLayout.LayoutParams temp_layoutParams = new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mColumnHeaderWidth), ViewGroup.LayoutParams.WRAP_CONTENT);
        temp_layoutParams.setMargins(0, ScreenUtils.dip2px(mContext, mCellMargin), 0, mCellMargin);
        temp_layoutParams.gravity = Gravity.CENTER;
        temp_tv.setGravity(Gravity.CENTER);
        temp_tv.setLayoutParams(temp_layoutParams);
        int columnHeaderHeight = measureTextHeight(temp_tv, headerTexts[3]) + 10;
        Log.e("Logs", "columnHeaderHeight:" + ScreenUtils.px2dip(mContext, columnHeaderHeight));
        mViewHeight = columnHeaderHeight;


        {
            //画分割线
            View v = new View(mContext);
            v.setBackgroundColor(mContext.getResources().getColor(R.color.seperateLineColor));
            LinearLayout.LayoutParams view_layoutParams =
                    new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mColumnHeaderWidth), ScreenUtils.dip2px(mContext, mSplitLineThick));
            v.setLayoutParams(view_layoutParams);
            ll_columnHeader.addView(v);
        }
        int emptyHeight=ScreenUtils.dip2px(mContext, mFirstRowHeight);
        int totalViewDividerHeight=0;
        /**
         * 循环添加第一列
         */
        for (int i = 0; i < headerTexts.length; i++) {
            String text = headerTexts[i];
            /**
             *
             *   创建一个新的TextView 把它加入到ColumnHeader 里去
             */
            TextView tv = new TextView(mContext);
            tv.setText(text);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, mColumnHeaderTextSize);

            LinearLayout.LayoutParams tv_layoutParams = new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mColumnHeaderWidth), columnHeaderHeight);
            tv.setPadding(ScreenUtils.dip2px(mContext, mCellPadding), 0, 0, 0);
            //tv_layoutParams.setMargins(0, ScreenUtils.dip2px(mContext, mCellMargin), 0, ScreenUtils.dip2px(mContext, mCellMargin));
            tv_layoutParams.gravity = Gravity.CENTER;
            tv.setGravity(Gravity.CENTER);
            tv.setLayoutParams(tv_layoutParams);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView tv = (TextView) v;

                    if (tv.getText().equals(headerTexts[0])) {
                        PlaqueDialog dialog = new PlaqueDialog(mContext, R.style.dialog_bleeding);
                        dialog.show();
                    } else if (tv.getText().equals(headerTexts[1])) {

                        ToothMobilityDialog dialog = new ToothMobilityDialog(mContext, R.style.dialog_bleeding);
                        dialog.show();

                    } else if (tv.getText().equals(headerTexts[2])) {
                        BleedingDialog dialog = new BleedingDialog(mContext, R.style.dialog_bleeding);
                        dialog.show();

                    } else if (tv.getText().equals(headerTexts[3])) {
                        PocketDialog dialog = new PocketDialog(mContext, R.style.dialog_bleeding);
                        dialog.show();

                    }

                }
            });
            /**
             *   TextView 的DrawableLeft
             */
            Drawable draw1 = mContext.getResources().getDrawable(getDrawableLeft(i));
            draw1.setBounds(0, 0,
                    ScreenUtils.dip2px(mContext, drawableWidth),
                    ScreenUtils.dip2px(mContext, drawableHeight));
            tv.setCompoundDrawables(draw1, null, null, null);
            ll_columnHeader.addView(tv);

            /**
             *   分割线
             */

            View v = new View(mContext);
            v.setBackgroundColor(mContext.getResources().getColor(R.color.seperateLineColor));
            LinearLayout.LayoutParams view_layoutParams =
                    new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mColumnHeaderWidth), ScreenUtils.dip2px(mContext, mSplitLineThick));

            //ToothLocation 双倍分割线
            if (i == 3 || i == 4) {

                view_layoutParams.height = ScreenUtils.dip2px(mContext, mSplitLineHeavyThick);
            }

            v.setLayoutParams(view_layoutParams);
            totalViewDividerHeight+=view_layoutParams.height;
            loadInfo(columnHeaderHeight*i+emptyHeight+totalViewDividerHeight);

            ll_columnHeader.addView(v);
        }

        {
            /**
             * 画尾部的空白
             */
            View v_header = new View(mContext);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mColumnHeaderWidth),
                    ScreenUtils.dip2px(mContext, mFirstRowHeight));
            //v_header.setBackgroundColor(Color.RED);
            v_header.setLayoutParams(layoutParams);
            ll_columnHeader.addView(v_header);
        }
    }

    void loadInfo(int itemHeight){
        RelativeLayout.LayoutParams iv_layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        iv_layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

        iv_layoutParams.setMargins(ScreenUtils.dip2px(mContext, mColumnHeaderWidth-16),itemHeight+ScreenUtils.dip2px(mContext, 2),0,0);
        ImageView imageView=new ImageView(mContext);
        imageView.setImageResource(R.mipmap.info_disease);
        imageView.setLayoutParams(iv_layoutParams);
        rl_columnInfoHeader.addView(imageView);

    }
    private void loadCells(HorizontalScrollView scrollView) {

        //不使用Inflater，因为否则LinearLayout 太多，会导致加载该View 变慢
        // LayoutInflater.from(mContext).inflate()

        LinearLayout ll_root = new LinearLayout(mContext); //创建根LL
        {
            /**
             * 初始化根LL
             */
            ll_root.setOrientation(LinearLayout.VERTICAL);
            HorizontalScrollView.LayoutParams layoutParams =
                    new HorizontalScrollView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            ll_root.setLayoutParams(layoutParams);
        }

        /**
         * 循环插入View
         */
        for (int i = 0; i < 11; i++) {

            if (i == 0) {
                //第一行
                LinearLayout linearLayout_outer = new LinearLayout(mContext);
                {
                    linearLayout_outer.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams params =
                            new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mViewWidth) * 16,
                                    ScreenUtils.dip2px(mContext, mFirstRowHeight));
                    linearLayout_outer.setLayoutParams(params);
                    ll_root.addView(linearLayout_outer);
                }

                LinearLayout linearLayout_inner1 = new LinearLayout(mContext);
                {
                    linearLayout_inner1.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams params =
                            new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mViewWidth) * 8
                                    , ScreenUtils.dip2px(mContext, mFirstRowHeight));
                    linearLayout_inner1.setLayoutParams(params);
                    linearLayout_outer.addView(linearLayout_inner1);

                    TextView textView = new TextView(mContext);
                    textView.setText(R.string.newString_4);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                    LinearLayout.LayoutParams tv_params =
                            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    tv_params.setMargins(ScreenUtils.dip2px(mContext, mViewWidth), 0, 0, 0);
                    tv_params.gravity = Gravity.CENTER;
                    textView.setLayoutParams(tv_params);
                    linearLayout_inner1.addView(textView);
                }

                LinearLayout linearLayout_inner2 = new LinearLayout(mContext);
                {
                    linearLayout_inner2.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams params =
                            new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mViewWidth) * 8,
                                    ScreenUtils.dip2px(mContext, mFirstRowHeight));
                    linearLayout_inner2.setLayoutParams(params);
                    linearLayout_outer.addView(linearLayout_inner2);

                    TextView textView = new TextView(mContext);
                    textView.setText(R.string.newString_5);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                    LinearLayout.LayoutParams tv_params =
                            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    tv_params.gravity = Gravity.CENTER;
                    tv_params.setMargins(ScreenUtils.dip2px(mContext, mViewWidth), 0, 0, 0);
                    textView.setLayoutParams(tv_params);
                    linearLayout_inner2.addView(textView);
                }

                //画第一行的分界线
                View v = new View(mContext);
                LinearLayout.LayoutParams lp_v =
                        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                ScreenUtils.dip2px(mContext, mSplitLineThick));
                v.setLayoutParams(lp_v);
                v.setBackgroundColor(mContext.getResources().getColor(R.color.seperateLineColor));
                ll_root.addView(v);

            } else if (i == 5) {
                //牙齿Location

                LinearLayout linearLayout_outer = new LinearLayout(mContext);
                {
                    linearLayout_outer.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams params =
                            new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mViewWidth) * 16,
                                    mViewHeight);
                    linearLayout_outer.setLayoutParams(params);
                    ll_root.addView(linearLayout_outer);
                }

                for (int j = 0; j < 16; j++) {
                    {
                        /**
                         * 画TextView Location
                         */
                        TextView v = new TextView(mContext);
                        LinearLayout.LayoutParams lp_v = new LinearLayout.LayoutParams(
                                ScreenUtils.dip2px(mContext, mViewWidth), mViewHeight);
                        v.setBackgroundColor(Color.WHITE);
                        v.setLayoutParams(lp_v);
                        v.setTextColor(Color.BLACK);
                        int position = j >= 8 ? j - 7 : 8 - j;
                        v.setText(position + "");
                        v.setGravity(Gravity.CENTER);
                        v.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
                        linearLayout_outer.addView(v);
                    }

                    {
                        /**
                         *  画右测的间隔线
                         */
                        if (j != 15) {

                            //画第一行的分界线
                            View v = new View(mContext);

                            LinearLayout.LayoutParams lp_v = new LinearLayout.LayoutParams(
                                    ScreenUtils.dip2px(mContext, mSplitLineThick), mViewHeight);

                            //如果是最中间的Line 双倍宽度
                            if (j == 7) {
                                lp_v.width = ScreenUtils.dip2px(mContext, mSplitLineHeavyThick);

                            }
                            v.setLayoutParams(lp_v);
                            v.setBackgroundColor(mContext.getResources().getColor(R.color.seperateLineColor));
                            linearLayout_outer.addView(v);

                        }
                    }
                }

                {
                    //画第末尾的分界线
                    View v = new View(mContext);
                    LinearLayout.LayoutParams lp_v =
                            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                    ScreenUtils.dip2px(mContext, mSplitLineHeavyThick));


                    v.setLayoutParams(lp_v);
                    v.setBackgroundColor(mContext.getResources().getColor(R.color.seperateLineColor));
                    ll_root.addView(v);
                }

            } else if (i == 10) {
                //最后一行
                LinearLayout linearLayout_outer = new LinearLayout(mContext);
                {
                    linearLayout_outer.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams params =
                            new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mViewWidth) * 16,
                                    ScreenUtils.dip2px(mContext, mFirstRowHeight));
                    linearLayout_outer.setLayoutParams(params);
                    ll_root.addView(linearLayout_outer);
                }

                LinearLayout linearLayout_inner1 = new LinearLayout(mContext);
                {
                    linearLayout_inner1.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams params =
                            new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mViewWidth) * 8
                                    , ScreenUtils.dip2px(mContext, mFirstRowHeight));
                    linearLayout_inner1.setLayoutParams(params);
                    linearLayout_outer.addView(linearLayout_inner1);

                    TextView textView = new TextView(mContext);
                    textView.setText(R.string.newString_6);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                    LinearLayout.LayoutParams tv_params =
                            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    tv_params.gravity = Gravity.CENTER;
                    tv_params.setMargins(ScreenUtils.dip2px(mContext, mViewWidth), 0, 0, 0);
                    textView.setLayoutParams(tv_params);
                    linearLayout_inner1.addView(textView);
                }

                LinearLayout linearLayout_inner2 = new LinearLayout(mContext);
                {
                    linearLayout_inner2.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams params =
                            new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mViewWidth) * 8,
                                    ScreenUtils.dip2px(mContext, mFirstRowHeight));
                    linearLayout_inner2.setLayoutParams(params);
                    linearLayout_outer.addView(linearLayout_inner2);

                    TextView textView = new TextView(mContext);
                    textView.setText(R.string.newString_7);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                    LinearLayout.LayoutParams tv_params =
                            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    tv_params.gravity = Gravity.CENTER;
                    tv_params.setMargins(ScreenUtils.dip2px(mContext, mViewWidth), 0, 0, 0);
                    textView.setLayoutParams(tv_params);
                    linearLayout_inner2.addView(textView);
                }

                //画第一行的分界线
                View v = new View(mContext);
                LinearLayout.LayoutParams lp_v =
                        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                ScreenUtils.dip2px(mContext, mSplitLineThick));
                v.setLayoutParams(lp_v);
                v.setBackgroundColor(mContext.getResources().getColor(R.color.seperateLineColor));
                ll_root.addView(v);

            } else {
                /**
                 * 画普通的Cell
                 */
                LinearLayout linearLayout_outer = new LinearLayout(mContext);
                {
                    linearLayout_outer.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams params =
                            new LinearLayout.LayoutParams(ScreenUtils.dip2px(mContext, mViewWidth) * 16,
                                    mViewHeight);
                    linearLayout_outer.setLayoutParams(params);
                    ll_root.addView(linearLayout_outer);
                }
                int real_i = i >= 5 ? i - 2 : i - 1;
                views[real_i] = new TextView[16];
                for (int j = 0; j < 16; j++) {
                    {
                        /**
                         * 画Cell
                         */
                        TextView v = new TextView(mContext);
                        LinearLayout.LayoutParams lp_v = new LinearLayout.LayoutParams(
                                ScreenUtils.dip2px(mContext, mViewWidth), mViewHeight);
                        v.setBackgroundColor(Color.WHITE);
                        v.setTextColor(Color.BLACK);
                        v.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                        v.setGravity(Gravity.CENTER);
                        v.setLayoutParams(lp_v);
                        v.setOnClickListener(this);
                        linearLayout_outer.addView(v);
                        views[real_i][j] = v;
                        v.setTag(real_i + "," + j); //i,j 来标识
                    }
                    {
                        /**
                         *  画右测的间隔线
                         */
                        if (j != 15) {

                            //画第一行的分界线
                            View v = new View(mContext);
                            LinearLayout.LayoutParams lp_v = new LinearLayout.LayoutParams(
                                    ScreenUtils.dip2px(mContext, mSplitLineThick), mViewHeight);

                            //如果是最中间的Line 双倍宽度
                            if (j == 7) {
                                lp_v.width = ScreenUtils.dip2px(mContext, mSplitLineHeavyThick);

                            }
                            v.setLayoutParams(lp_v);
                            v.setBackgroundColor(mContext.getResources().getColor(R.color.seperateLineColor));
                            linearLayout_outer.addView(v);
                        }
                    }
                }

                //画第一行的分界线
                View v = new View(mContext);
                LinearLayout.LayoutParams lp_v =
                        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                ScreenUtils.dip2px(mContext, mSplitLineThick));
                if (i == 4) {
                    lp_v.height = ScreenUtils.dip2px(mContext, mSplitLineHeavyThick);
                }

                v.setLayoutParams(lp_v);
                v.setBackgroundColor(mContext.getResources().getColor(R.color.seperateLineColor));
                ll_root.addView(v);
            }
        }
        scrollView.addView(ll_root);

    }

    /**
     * 根据index 的值来获取小图标
     *
     * @param index
     * @return
     */
    private int getDrawableLeft(int index) {

        switch (index) {
            case 0:
                return R.mipmap.icon_num1;

            case 1:
                return R.mipmap.icon_num2;

            case 2:
                return R.mipmap.icon_num3;
            case 3:
                return R.mipmap.icon_num4;

            case 4:
                return R.mipmap.icon_num5;

            case 5:
                return R.mipmap.icon_num4;

            case 6:
                return R.mipmap.icon_num3;
            case 7:
                return R.mipmap.icon_num2;

            case 8:
                return R.mipmap.icon_num1;

            default:
                return 0;
        }
    }

    public void show() {

        //设置ColumnHeader 最小高度
        minRowHeight = ScreenUtils.dip2px(mContext, 40);
        //设置ColumnHeader 最大高度
        maxRowHeight = ScreenUtils.dip2px(mContext, 50);

        loadColumnHeader(ll_columnHeader);
        loadCells(mScrollView);
        mContainerView.addView(mTableView);
        mContainerView.invalidate();

    }

    int minColumnWidth;
    int maxColumnWidth;

    /**
     * 根据最大最小值，计算TextView的宽度
     *
     * @param textView
     * @param text
     * @return
     */
    private int measureTextWidth(TextView textView, String text) {
        if (textView != null) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
            int width = DisplayUtil.px2dip(mContext, layoutParams.leftMargin) +
                    DisplayUtil.px2dip(mContext, layoutParams.rightMargin) +
                    getTextViewWidth(textView, text);
            if (width <= minColumnWidth) {
                return minColumnWidth;
            } else if (width > minColumnWidth && width <= maxColumnWidth) {
                return width;
            } else {
                return maxColumnWidth;
            }
        }
        return 0;
    }

    private void initDisplayOpinion() {
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        DisplayUtil.density = dm.density;
        DisplayUtil.densityDPI = dm.densityDpi;
        DisplayUtil.screenWidthPx = dm.widthPixels;
        DisplayUtil.screenhightPx = dm.heightPixels;
        DisplayUtil.screenWidthDip = DisplayUtil.px2dip(mContext, dm.widthPixels);
        DisplayUtil.screenHightDip = DisplayUtil.px2dip(mContext, dm.heightPixels);
    }

    int minRowHeight;
    int maxRowHeight;

    /**
     * 计算TextView高度
     *
     * @param textView
     * @param text
     * @return
     */
    private int measureTextHeight(TextView textView, String text) {
        if (textView != null) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
            int height = getTextViewHeight(textView, text);
            if (height < minRowHeight) {
                return minRowHeight;
            } else if (height > minRowHeight && height < maxRowHeight) {
                return height;
            } else {
                return maxRowHeight;
            }
        }
        return 0;
    }

    /**
     * 根据文字计算textview的高度
     *
     * @param textView
     * @param text
     * @return
     */
    private int getTextViewHeight(TextView textView, String text) {
        if (textView != null) {
            int width = measureTextWidth(textView, text);
            TextPaint textPaint = textView.getPaint();
            StaticLayout staticLayout = new StaticLayout(text, textPaint, DisplayUtil.dip2px(mContext, width), Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
            int height = DisplayUtil.px2dip(mContext, staticLayout.getHeight());
            return height;
        }
        return 0;
    }

    /**
     * 说明 根据文字和指定宽度计算高度
     * 作者 郭翰林
     * 创建时间 2018/3/15 下午12:39
     *
     * @param textView
     * @param text
     * @param width
     * @return
     */
    private int getTextViewHeight(TextView textView, String text, int width) {
        if (textView != null) {
            TextPaint textPaint = textView.getPaint();
            StaticLayout staticLayout = new StaticLayout(text, textPaint, DisplayUtil.dip2px(mContext, width), Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
            int height = DisplayUtil.px2dip(mContext, staticLayout.getHeight());
            return height;
        }
        return 0;
    }

    /**
     * 根据文字计算textview的高度
     *
     * @param view
     * @param text
     * @return
     */
    private int getTextViewWidth(TextView view, String text) {
        if (view != null) {
            TextPaint paint = view.getPaint();
            return DisplayUtil.px2dip(mContext, (int) paint.measureText(text));
        }
        return 0;
    }

    @Override
    public void onClick(View v) {

        // Toast.makeText(mContext, v.getTag().toString(), Toast.LENGTH_SHORT).show();
        if (mOnclickLiastener != null) {
            mOnclickLiastener.onClick(v);
        }

    }

    public static final int TYPE_NORMAL = 0x0;
    public static final int TYPE_PLAQUE = 0x1;
    public static final int TYPE_BLEEDING = 0x2;
    public static final int TYPE_ABSCESS = 0x3;
    public static final int TYPE_ABSCESS_PLUS_BLEEDING = 0x4;


    public void setCellViewBackground(int row, int column, int type) {

        if (type == TYPE_NORMAL) {
            views[row][column].setBackgroundColor(Color.WHITE);

        } else if (type == TYPE_PLAQUE) {

            views[row][column].setBackground(mContext.getResources().getDrawable(R.mipmap.color_plaque2));

        } else if (type == TYPE_BLEEDING) {

            views[row][column].setBackground(mContext.getResources().getDrawable(R.mipmap.color_bleeding2));

        } else if (type == TYPE_ABSCESS) {

            views[row][column].setBackground(mContext.getResources().getDrawable(R.mipmap.color_abscess));

        } else if (type == TYPE_ABSCESS_PLUS_BLEEDING) {

            views[row][column].setBackground(mContext.getResources().getDrawable(R.mipmap.color_bleeding_abscess));

        }


    }

    public TextView getCellView(int row, int column) {

        return views[row][column];
    }

    public void setValue(List<PlaqueRecord> list) {

        // type:0=Plaque,   type:1=toothMobility type,
        // type:2=Bleeding, type:3=Pocket depth

        for (int i = 0; i < list.size(); i++) {

            PlaqueRecord record = list.get(i);

            //Plaque
            if (record.getType() == 0) {
                setPlaque(record);
            } else if (record.getType() == 1) {
                setMobility(record);
            } else if (record.getType() == 2) {
                setBleeding(record);
            } else if (record.getType() == 3) {
                setPocket(record);
            }

        }
    }

    private void setBleeding(PlaqueRecord record) {

        setBleeding(record.getValue1(), 2, 0);
        setBleeding(record.getValue2(), 2, 1);
        setBleeding(record.getValue3(), 2, 2);
        setBleeding(record.getValue4(), 2, 3);
        setBleeding(record.getValue5(), 2, 4);
        setBleeding(record.getValue6(), 2, 5);
        setBleeding(record.getValue7(), 2, 6);
        setBleeding(record.getValue8(), 2, 7);
        setBleeding(record.getValue9(), 2, 8);
        setBleeding(record.getValue10(), 2, 9);
        setBleeding(record.getValue11(), 2, 10);
        setBleeding(record.getValue12(), 2, 11);
        setBleeding(record.getValue13(), 2, 12);
        setBleeding(record.getValue14(), 2, 13);
        setBleeding(record.getValue15(), 2, 14);
        setBleeding(record.getValue16(), 2, 15);

        setBleeding(record.getValue17(), 5, 0);
        setBleeding(record.getValue18(), 5, 1);
        setBleeding(record.getValue19(), 5, 2);
        setBleeding(record.getValue20(), 5, 3);
        setBleeding(record.getValue21(), 5, 4);
        setBleeding(record.getValue22(), 5, 5);
        setBleeding(record.getValue23(), 5, 6);
        setBleeding(record.getValue24(), 5, 7);
        setBleeding(record.getValue25(), 5, 8);
        setBleeding(record.getValue26(), 5, 9);
        setBleeding(record.getValue27(), 5, 10);
        setBleeding(record.getValue28(), 5, 11);
        setBleeding(record.getValue29(), 5, 12);
        setBleeding(record.getValue30(), 5, 13);
        setBleeding(record.getValue31(), 5, 14);
        setBleeding(record.getValue32(), 5, 15);
    }

    private void setBleeding(String value, int row, int column) {

        int value1 = 0;
        if (value == null || value.equals("")) {
            value1 = 0;
        } else {
            value1 = Integer.parseInt(value);
        }

        if (value1 == 0) {
            views[row][column].setBackgroundColor(Color.WHITE);
        } else if (value1 == 2) {
            views[row][column].setBackground(mContext.getResources().getDrawable(R.mipmap.color_bleeding2));
        } else if (value1 == 3) {
            views[row][column].setBackground(mContext.getResources().getDrawable(R.mipmap.color_abscess));
        } else {
            views[row][column].setBackground(mContext.getResources().getDrawable(R.mipmap.color_bleeding_abscess));
        }

    }

    private void setMobility(PlaqueRecord record) {
        views[1][0].setText(record.getValue1());
        views[1][1].setText(record.getValue2());
        views[1][2].setText(record.getValue3());
        views[1][3].setText(record.getValue4());
        views[1][4].setText(record.getValue5());
        views[1][5].setText(record.getValue6());
        views[1][6].setText(record.getValue7());
        views[1][7].setText(record.getValue8());
        views[1][8].setText(record.getValue9());
        views[1][9].setText(record.getValue10());
        views[1][10].setText(record.getValue11());
        views[1][11].setText(record.getValue12());
        views[1][12].setText(record.getValue13());
        views[1][13].setText(record.getValue14());
        views[1][14].setText(record.getValue15());
        views[1][15].setText(record.getValue16());
        views[6][0].setText(record.getValue17());
        views[6][1].setText(record.getValue18());
        views[6][2].setText(record.getValue19());
        views[6][3].setText(record.getValue20());
        views[6][4].setText(record.getValue21());
        views[6][5].setText(record.getValue22());
        views[6][6].setText(record.getValue23());
        views[6][7].setText(record.getValue24());
        views[6][8].setText(record.getValue25());
        views[6][9].setText(record.getValue26());
        views[6][10].setText(record.getValue27());
        views[6][11].setText(record.getValue28());
        views[6][12].setText(record.getValue29());
        views[6][13].setText(record.getValue30());
        views[6][14].setText(record.getValue31());
        views[6][15].setText(record.getValue32());
    }


    private void setPocket(PlaqueRecord record) {

        setPocketText(views[3][0], record.getValue1());
        setPocketText(views[3][1], record.getValue2());
        setPocketText(views[3][2], record.getValue3());
        setPocketText(views[3][3], record.getValue4());
        setPocketText(views[3][4], record.getValue5());
        setPocketText(views[3][5], record.getValue6());
        setPocketText(views[3][6], record.getValue7());
        setPocketText(views[3][7], record.getValue8());
        setPocketText(views[3][8], record.getValue9());
        setPocketText(views[3][9], record.getValue10());
        setPocketText(views[3][10], record.getValue11());
        setPocketText(views[3][11], record.getValue12());
        setPocketText(views[3][12], record.getValue13());
        setPocketText(views[3][13], record.getValue14());
        setPocketText(views[3][14], record.getValue15());
        setPocketText(views[3][15], record.getValue16());
        setPocketText(views[4][0], record.getValue17());
        setPocketText(views[4][1], record.getValue18());
        setPocketText(views[4][2], record.getValue19());
        setPocketText(views[4][3], record.getValue20());
        setPocketText(views[4][4], record.getValue21());
        setPocketText(views[4][5], record.getValue22());
        setPocketText(views[4][6], record.getValue23());
        setPocketText(views[4][7], record.getValue24());
        setPocketText(views[4][8], record.getValue25());
        setPocketText(views[4][9], record.getValue26());
        setPocketText(views[4][10], record.getValue27());
        setPocketText(views[4][11], record.getValue28());
        setPocketText(views[4][12], record.getValue29());
        setPocketText(views[4][13], record.getValue30());
        setPocketText(views[4][14], record.getValue31());
        setPocketText(views[4][15], record.getValue32());
    }

    private void setPocketText(TextView tv, String value) {

        if (value == null) {
            tv.setText("");
            return;
        }


        if (value.equals("n/a")) {
            tv.setTextColor(mContext.getResources().getColor(R.color.seperateLineColor));

        } else {
            tv.setTextColor(Color.parseColor("#666666"));
        }
        tv.setText(value);

    }

    private void setPlaque(PlaqueRecord record) {
        //0 : healthy , 1:plaque
        setPlaque(record.getValue1(), 0, 0);
        setPlaque(record.getValue2(), 0, 1);
        setPlaque(record.getValue3(), 0, 2);
        setPlaque(record.getValue4(), 0, 3);
        setPlaque(record.getValue5(), 0, 4);
        setPlaque(record.getValue6(), 0, 5);
        setPlaque(record.getValue7(), 0, 6);
        setPlaque(record.getValue8(), 0, 7);
        setPlaque(record.getValue9(), 0, 8);
        setPlaque(record.getValue10(), 0, 9);
        setPlaque(record.getValue11(), 0, 10);
        setPlaque(record.getValue12(), 0, 11);
        setPlaque(record.getValue13(), 0, 12);
        setPlaque(record.getValue14(), 0, 13);
        setPlaque(record.getValue15(), 0, 14);
        setPlaque(record.getValue16(), 0, 15);

        setPlaque(record.getValue17(), 7, 0);
        setPlaque(record.getValue18(), 7, 1);
        setPlaque(record.getValue19(), 7, 2);
        setPlaque(record.getValue20(), 7, 3);
        setPlaque(record.getValue21(), 7, 4);
        setPlaque(record.getValue22(), 7, 5);
        setPlaque(record.getValue23(), 7, 6);
        setPlaque(record.getValue24(), 7, 7);
        setPlaque(record.getValue25(), 7, 8);
        setPlaque(record.getValue26(), 7, 9);
        setPlaque(record.getValue27(), 7, 10);
        setPlaque(record.getValue28(), 7, 11);
        setPlaque(record.getValue29(), 7, 12);
        setPlaque(record.getValue30(), 7, 13);
        setPlaque(record.getValue31(), 7, 14);
        setPlaque(record.getValue32(), 7, 15);
    }

    private void setPlaque(String value, int row, int column) {
        int value1 = 0;
        if (value == null || value.equals("")) {
            value1 = 0;
        } else {
            value1 = Integer.parseInt(value);
        }

        if (value1 == 0) {
            views[row][column].setBackgroundColor(Color.WHITE);
        } else {
            views[row][column].setBackground(mContext.getResources().getDrawable(R.mipmap.color_plaque2));
        }
    }
}
